module.exports = {
    // 补零
    repairZero: s=> (s+'').length === 1 ? '0' + s : s,
    // 世界时
    UTC(){
    	const date = new Date();
    	return `${date.getUTCFullYear()}-${this.repairZero(date.getUTCMonth() + 1 )}-${this.repairZero(date.getUTCDate())} ${this.repairZero(date.getUTCHours())}:${this.repairZero(date.getUTCMinutes())}:${this.repairZero(date.getUTCSeconds())}`
    },
    UTCTime(){
    	const date = new Date();
    	return date.getTime() + date.getTimezoneOffset() * 60 * 1000;
    },
    // 本地时
    local(type,dateObj){
    	dateObj = dateObj || new Date();
    	const [date,time] = [
    		`${dateObj.getFullYear()}-${this.repairZero(dateObj.getMonth() + 1 )}-${this.repairZero(dateObj.getDate())}`,
    		`${this.repairZero(dateObj.getHours())}:${this.repairZero(dateObj.getMinutes())}:${this.repairZero(dateObj.getSeconds())}`
    	]
    	
    	switch(type){
    		case 'date': return date;
    		case 'time': return time;
    		default: return date + ' ' + time;
    	}
    },
    // 获取某月份多少天
    getDateLen(date,month){
    	let year;
    	if( base.isType(date) === 'date'){
    		year = date.getFullYear();
    		month = date.getMonth() + 1;
    	}
    	else year = date;
    	month += '';
    	return month.replace(/12|10|1|3|5|7|8/,'') ? (  month.replace(/11|4|6|9/,'') ? ( !(year % 4) && year % 100 || !(year % 400) ?  29 : 28):  30  )  : 31;
    },
    
    // 格式化时间
    format(s,date){
    	date = date || new Date();
    	const {repairZero} = this;
    	const reg = {
    		yyyy: date.getFullYear(),
    		yy: (date.getFullYear()+'').slice(2),
    		MM: repairZero( date.getMonth() + 1),
    		dd: repairZero( date.getDate()),
    		HH: repairZero( date.getHours()),
    		mm: repairZero( date.getMinutes()),
    		ss: repairZero( date.getSeconds()), 
    	}
    	Object.keys(reg).forEach(r=> s = s.replace( new RegExp(`${r}`,'g' ), reg[r] ) ) 
    	return s
    },
}